Unit atendimento_unt;

interface

uses
  Funcoes,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBCtrls, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids, DB,
  ComCtrls, Mask, IBCustomDataSet, Menus, IBQuery, IBTable,
  DateUtil, DateUtils;

type
  Tatendimento_frm = class(TForm)
    IBQBuscaDomicilio: TIBQuery;
    IBQTemp: TIBQuery;
    GroupBox1: TGroupBox;
    Label7: TLabel;
    Label8: TLabel;
    DBEdit4: TDBEdit;
    DBEdit5: TDBEdit;
    btnGravar: TButton;
    btnFechar: TButton;
    dsBuscaDomicilio: TDataSource;
    GroupBox2: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBEdit7: TDBEdit;
    DBEdit8: TDBEdit;
    DBEdit9: TDBEdit;
    DBMemo1: TDBMemo;
    Label1: TLabel;
    DSAtendimento: TDataSource;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label12: TLabel;
    DSAuxiliarAtendimento: TDataSource;
    dblcAuxiliarAtendimento: TDBLookupComboBox;
    Label13: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure btnFecharClick(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBMemo1Enter(Sender: TObject);
    procedure DBMemo1KeyPress(Sender: TObject; var Key: Char);
    procedure DBMemo1Exit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure ProcessaMsg(var Msg: TMsg; var Handled: Boolean);
  public
    { Public declarations }
  end;

var
  atendimento_frm: Tatendimento_frm;
  idPessoa: Integer;
  idAtendimento: Integer;

implementation

{$R *.dfm}

Uses cadastrodomicilios_unt, principal_unt, DMSistema_unt, DMDados_unt;

procedure Tatendimento_frm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
   DMDados_frm.IBQAuxiliarAtendimento.Close;
   with cadastrodomicilios_frm.IBQAtendimentos do
   begin
      try
         close;
         parambyname('domicilio').asinteger := DMDados_frm.IBDSDomicilioIDDOMICILIO.Value;
         prepare;
         open;
      except
      end;
   end;
   atendimento_frm := nil;
   action := caFree;
end;

procedure Tatendimento_frm.ProcessaMsg(var Msg: TMsg;
  var Handled: Boolean);
begin
   if Msg.message = WM_KEYDOWN then
      if not (Screen.ActiveControl is TCustomMemo) and not (Screen.ActiveControl is TButtonControl) then
      begin
         if not (Screen.ActiveControl is TCustomControl) then
         begin
            if Msg.wParam = VK_Down then Msg.wParam := VK_Tab;
            if Msg.wParam = VK_UP then
            begin
               Msg.wParam := VK_CLEAR;
               Screen.ActiveForm.Perform(WM_NextDlgCtl,1,0);
            end;
         end;
         if Msg.wParam = VK_Return then Msg.wParam := VK_Tab;
      end;
end;

procedure Tatendimento_frm.FormShow(Sender: TObject);
var
   dia,mes,ano: Word;
   hora,minuto,segundo,milisegundo: Word;
begin
   DecodeDate(dataatual,ano,mes,dia);
   DecodeTime(horaatual,hora,minuto,segundo,milisegundo);
   Application.OnMessage := ProcessaMsg;
   Label5.Caption := '0000';
   with IBQTemp do
   begin
      try
         Close;
         SQL.Clear;
         SQL.Add('select coalesce(idpessoa,0) from pessoas where iddomicilio = :domicilio and idrelacaorf = 1');
         ParamByName('domicilio').asinteger := DMDados_frm.IBDSDomicilioIDDOMICILIO.Value;
         Prepare;
         Open;
      except
      end;
   end;
   idPessoa := IBQTemp.Fields[0].Value;
   with DMDados_frm.IBQAuxiliarAtendimento do
   begin
      try
         prepare;
         open;
      except
      end;
   end;
   with DMDados_frm.IBDSAtendimento do
   begin
      try
         close;
         prepare;
         open;
         fetchall;
         first;
      except
      end;
   end;
   DMDados_frm.IBDSAtendimento.Insert;
   with IBQTemp do
   begin
      try
         Close;
         SQL.Clear;
         SQL.Add('select coalesce(gen_id(GEN_ATENDIMENTO_ID,0),0) as codigo from RDB$DATABASE');
         Prepare;
         Open;
      except
      end;
   end;
   idAtendimento := IBQTemp.Fields[0].Value + 1;
   DMDados_frm.IBDSAtendimentoIDATENDIMENTO.Value := idAtendimento;
   DMDados_frm.IBDSAtendimentoIDDOMICILIO.Value := DMDados_frm.IBDSDomicilioIDDOMICILIO.Value;
   DMDados_frm.IBDSAtendimentoIDPESSOA.Value := idPessoa;
   DMDados_frm.IBDSAtendimentoDATA.Value := dataatual;
   DMDados_frm.IBDSAtendimentoHORA.Value := horaatual;
   DMDados_frm.IBDSAtendimentoIDUSUARIO.Value := codusuario;
   DMDados_frm.IBDSAtendimentoIDAUXILIARATEND.Value := 0;
   DMDados_frm.IBDSAtendimentoCHAVEATENDIMENTO.Value := PadL(PadL(IntToStr(ano),4,'0')+PadL(IntToStr(mes),2,'0')+PadL(IntToStr(dia),2,'0')+PadL(IntToStr(hora),2,'0')+PadL(IntToStr(minuto),2,'0'),13,'0');
   with IBQBuscaDomicilio do
   begin
      try
         Close;
         ParamByName('iddomicilio').asinteger := DMDados_frm.IBDSDomicilioIDDOMICILIO.Value;
         Prepare;
         Open;
      except
      end;
   end;
   dblcAuxiliarAtendimento.SetFocus;
end;

procedure Tatendimento_frm.btnFecharClick(Sender: TObject);
begin
   close;
end;

procedure Tatendimento_frm.btnGravarClick(Sender: TObject);
var
   contador: Integer;
   chave1, chave2: String;
   dia,mes,ano: Word;
   hora,minuto,segundo,milisegundo: Word;
begin
   if (DMDados_frm.IBDSAtendimento.State in [dsInsert]) then
   begin
      contador := 0;
      DecodeDate(dataatual,dia,mes,ano);
      DecodeTime(horaatual,hora,minuto,segundo,milisegundo);
      chave1 := 'A'+
                PadL(Copy(IntToStr(ano),3,2),2,'0')+
                PadL(IntToStr(mes),2,'0')+
                PadL(IntToStr(dia),2,'0')+
                PadL(IntToStr(hora),2,'0')+
                PadL(IntToStr(minuto),2,'0');
      with IBQTemp do
      begin
         try
            Close;
            SQL.Clear;
            SQL.Add('select idatendimento from atendimento where chaveatendimento = :chave');
         except
         end;
      end;
      while not (IBQTemp.IsEmpty) do
      begin
         Inc(contador);
         chave2 := chave1+PadL(IntToStr(contador),2,'0');
         with IBQTemp do
         begin
            try
               Close;
               ParamByName('chave').AsString := chave2;
               Prepare;
               Open;
            except
            end;
         end;
      end;
      DMDados_frm.IBDSAtendimentoCHAVEATENDIMENTO.Value := chave2;
   end;
   with DMDados_frm.IBDSAtendimento do
   begin
      try
         Post;
         Close;
         ParamByName('domicilio').AsInteger := DMDados_frm.IBDSDomicilioIDDOMICILIO.Value;
         Prepare;
         Open;
      except
      end;
   end;
   Localiza(Self,DMDados_frm.IBDSAtendimento,'IDATENDIMENTO',IntToStr(idAtendimento));
   Messagedlg('Atendimento registrado com sucesso.',MtInformation,[mbok],0);
   btnFecharClick(Self);
end;

procedure Tatendimento_frm.FormKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
   Case Key of
      VK_RETURN            : if not (Screen.ActiveControl is TCustomMemo) then selectnext(twincontrol(sender),true,true);
      VK_ESCAPE            : If (DMDados_frm.IBDSAtendimento.State in ([dsInsert, dsEdit])) then
                                btnFecharClick(Self);
      VK_F5                : If (DMDados_frm.IBDSAtendimento.State in ([dsInsert, dsEdit])) then
                                btnGravarClick(Self);
   end;
end;

procedure Tatendimento_frm.DBMemo1Enter(Sender: TObject);
begin
   if (Length(DBMemo1.Text) > 0) then
   begin
      if (Length(DBMemo1.Text) >= 2400) then Label5.Caption := '0000'
      else Label5.Caption := PadL(IntToStr((DBMemo1.MaxLength-(Length(DBMemo1.Text)+1))),4,'0');
   end
   else Label5.Caption := '2400';
end;

procedure Tatendimento_frm.DBMemo1KeyPress(Sender: TObject; var Key: Char);
begin
   Key := AnsiUpperCase(MudaNome(Key))[1];
   if Length(DBMemo1.Text) >= 2400 then
   begin
      MessageDlg('Atingiu o limite do texto.',mtInformation,[mbOK],0);
      btnGravar.SetFocus;
   end
   else
   begin
      Label5.Caption := PadL(IntToStr((DBMemo1.MaxLength-(Length(DBMemo1.Text)+1))),4,'0');
   end;
end;

procedure Tatendimento_frm.DBMemo1Exit(Sender: TObject);
begin
   if (Length(DBMemo1.Text) > 0) then
   begin
      if (Length(DBMemo1.Text) >= 2400) then Label5.Caption := '0000'
      else Label5.Caption := PadL(IntToStr((DBMemo1.MaxLength-(Length(DBMemo1.Text)+1))),4,'0');
   end
   else Label5.Caption := '2400';
end;

procedure Tatendimento_frm.FormCreate(Sender: TObject);
begin
   //AjustaResolucao(Self, 1024);
end;

initialization
   iddomicilio := 0;
   idPessoa    := 0;
   idAtendimento := 0;

end.

